import { createWebHistory, createRouter } from 'vue-router'
import { admin ,user } from '@/api'
import { ElNotification } from 'element-plus'

const routes = [
  { path: '/', component: ()=>import("../layout/Login/index.vue")},
  {path:'/home',component:()=>import("../layout/index.vue"),children:[
    {path:'test',component:()=>import("../views/test/index.vue")},//测试
    {path:'panel',component:()=>import("../views/panel/index.vue")},//仪表盘
    {path:'useredit',component:()=>import("../views/useredit/index.vue")},//用户编辑
    {path:'amdinedit',component:()=>import("../views/amdinedit/index.vue")},//管理员编辑
    {path:'commentmanagement',component:()=>import("../views/commentmanagement/index.vue")},//评论管理
    {path:'postedit',component:()=>import("../views/postedit/index.vue")},//文章管理
    {path:'useractivitycount',component:()=>import("../views/useractivitycount/index.vue")},//用户活跃度报表
    {path:'articleviewcount',component:()=>import("../views/articleviewcount/index.vue")},//文章访问量报表
    {path:'savedata',component:()=>import("../views/savedata/index.vue")},//数据备份
    {path:'datarepair',component:()=>import("../views/datarepair/index.vue")},//数据恢复
    {path:'usehelp',component:()=>import("../views/usehelp/index.vue")},//使用手册
    {path:'Q&A',component:()=>import("../views/Q&A/index.vue")},//常见问题解答
    {path:'support',component:()=>import("../views/support/index.vue")},//联系支持
    {path:'information',component:()=>import("../views/information/index.vue")},//账号信息
    {path:'pwdchange',component:()=>import("../views/pwdchange/index.vue")},//密码修改
    {path:'category',component:()=>import("../views/category/index.vue")},//分类编辑
    {path:'tag',component:()=>import("../views/tag/index.vue")},//标签编辑
    {path:'addpost',component:()=>import("../views/addpost/index.vue")},//新增文章
    {path:'favorite',component:()=>import("../views/favorite/index.vue")},//收藏模块
    
  ]},
   {path:'/user',component:()=>import("../layout/User/index.vue")},//用户首页
   {path:'/user/self',component:()=>import("../layout/User/userself.vue"),children:[//个人设置
    {path:'myfav',component:()=>import("../layout/User/userself/myfav.vue")},//我的收藏
	{path:'mydrafts',component:()=>import("../layout/User/userself/luodrafts.vue")},//草稿箱
	{path:'mycomment',component:()=>import("../layout/User/userself/mycomment.vue")},//我的评论
	{path:'mypost',component:()=>import("../layout/User/userself/mypost.vue")}//我的文章
   ]},//个人中心
   {path:'/user/set',component:()=>import("../layout/User/userset.vue")},
   {path:'/user/post/:postId',name: 'post',component:()=>import("../layout/User/postdetail.vue")},//文章详细页
   {path:'/user/addpost/',name: 'addpost',component:()=>import("../layout/User/addpost.vue")},//文章详细页
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})



//导航守卫
router.beforeEach((to, from, next) => {
  // 获取当前路由的path
  const path = to.path;

  // 假设根据路由路径判断是后台还是前台路由
  const isAdminRoute = path.startsWith('/home');
  const isUserRoute = path.startsWith('/user');

  // 放行登录页面
  if (path === '/'|| path === '/user' || path==='/user/post') {
    next();
  }else{
    if (isAdminRoute) {
      // 判断是否是后台页面
      console.log("前往后台页面");
      admin.verifyToken().then(res=>{
        next()
      }).catch(err=>{
        ElNotification({
          title: 'Warning',
          message: 'token失效，请重新登陆',
          type: 'warning',
        })
        next('/')
      })
    }else if (isUserRoute) {
      // 判断是否是前台页面
      console.log("前往前台页面");
      user.verifyToken().then(res=>{
        next()
      }).catch(err=>{
        ElNotification({
          title: 'Warning',
          message: 'token失效，请重新登陆',
          type: 'warning',
        })
        next('/user')
      })
    }else{
      next('/user')
    }
  }
})

export default router;